package com.kclm.xsap.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kclm.xsap.consts.OperateType;
import com.kclm.xsap.dao.*;
import com.kclm.xsap.entity.*;
import com.kclm.xsap.service.*;
import com.kclm.xsap.utils.R;
import com.kclm.xsap.utils.exception.GlobalExceptionControllerAdvice;
import com.kclm.xsap.utils.exception.RRException;
import com.kclm.xsap.utils.exception.ServiceException;
import com.kclm.xsap.vo.ConsumeFormVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.List;

@Service
public class MemberCardServiceImpl extends ServiceImpl<MemberCardDao, MemberCardEntity> implements MemberCardService {
    @Autowired
    private MemberBindRecordDao memberBindRecordDao;
    @Autowired
    private MemberLogDao memberLogDao;
    @Autowired
    private ClassRecordDao classRecordDao;
    @Autowired
    private ConsumeRecordDao consumeRecordDao;
    @Autowired
    private ReservationRecordDao reservationRecordDao;
    @Autowired
    private RechargeRecordDao rechargeRecordDao;

    //扣费
    @Override
    @Transactional(rollbackFor = Exception.class)//sql语句回滚
    public boolean consum(ConsumeFormVo consumeFormVo) {
        System.out.println("consumeFormVo===" + consumeFormVo);
        MemberBindRecordEntity memberBindRecordEntity = memberBindRecordDao.selectById(consumeFormVo.getCardBindId());
        int validCount = memberBindRecordEntity.getValidCount() - consumeFormVo.getCardCountChange();
        BigDecimal validMoney = memberBindRecordEntity.getReceivedMoney().subtract(consumeFormVo.getAmountOfConsumption());
        if (validCount >= 0 && validMoney.compareTo(BigDecimal.ZERO) >= 0) {
            //添加操作记录 member_log
            MemberLogEntity memberLogEntity = null;
            try {
                memberLogEntity = new MemberLogEntity();
                memberLogEntity.setType(String.valueOf(OperateType.CLASS_DEDUCTION_OPERATION.getMsg()))
                        .setInvolveMoney(consumeFormVo.getAmountOfConsumption())
                        .setOperator(consumeFormVo.getOperator())
                        .setMemberBindId(consumeFormVo.getCardBindId())
                        .setCardCountChange(consumeFormVo.getCardCountChange())
                        .setNote(consumeFormVo.getNote());
                memberLogDao.insert(memberLogEntity);
            } catch (Exception e) {
                throw new ServiceException("添加操作记录失败！");
            }


            //添加消费记录 consume_record
            try {
                ConsumeRecordEntity consumeRecordEntity = new ConsumeRecordEntity();
                consumeRecordEntity.setOperateType(String.valueOf(OperateType.CLASS_DEDUCTION_OPERATION.getMsg()))
                        .setCardCountChange(consumeFormVo.getCardCountChange())
                        .setMoneyCost(consumeFormVo.getAmountOfConsumption())
                        .setOperator(consumeFormVo.getOperator())
                        .setNote(consumeFormVo.getNote())
                        .setScheduleId(consumeFormVo.getScheduleId())
                        .setMemberBindId(consumeFormVo.getCardBindId())
                        .setLogId(memberLogEntity.getId());
                consumeRecordDao.insert(consumeRecordEntity);
            } catch (Exception e) {
                throw new ServiceException("添加消费记录失败！");
            }


            //减次数 member_bind_record
            try {
                memberBindRecordEntity.setValidCount(memberBindRecordEntity.getValidCount() - consumeFormVo.getCardCountChange());
                memberBindRecordEntity.setReceivedMoney(memberBindRecordEntity.getReceivedMoney().subtract(consumeFormVo.getAmountOfConsumption()));
                memberBindRecordDao.updateById(memberBindRecordEntity);
            } catch (Exception e) {
                throw new ServiceException("更新会员卡信息失败！");
            }


            //更新上课记录的状态 class_record
            try {
                ClassRecordEntity classRecordEntity = classRecordDao.selectOne(new QueryWrapper<ClassRecordEntity>().eq("member_id", consumeFormVo.getMemberId())
                        .eq("schedule_id", consumeFormVo.getScheduleId())
                        .eq("bind_card_id", consumeFormVo.getCardBindId())
                        .eq("check_status", 0));
                classRecordEntity.setCheckStatus(1);
                classRecordDao.updateById(classRecordEntity);
            } catch (Exception e) {
                throw new RRException("更新上课记录状态失败！");
            }
            //更新已预约记录 reservation_record
            try {
                ReservationRecordEntity reservationRecordEntity = reservationRecordDao.selectOne(new QueryWrapper<ReservationRecordEntity>()
                        .eq("member_id", consumeFormVo.getMemberId())
                        .eq("schedule_id", consumeFormVo.getScheduleId())
                        .eq("card_id", consumeFormVo.getCardBindId()));
                System.out.println("consumeFormVo.getMemberId()" + consumeFormVo.getMemberId());
                System.out.println("consumeFormVo.getScheduleId()" + consumeFormVo.getScheduleId());

                System.out.println(reservationRecordEntity);
                reservationRecordEntity.setStatus(1);
                reservationRecordDao.updateById(reservationRecordEntity);
            } catch (Exception e) {
                throw new RRException("更新已预约记录状态失败！");
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean isInfo(RechargeRecordEntity rechargeRecordEntity) {
        //recharge保存
        try {
            rechargeRecordDao.insert(rechargeRecordEntity);
        } catch (Exception e) {
            throw new RRException("充值记录保存失败");
        }
        //赋值
        MemberBindRecordEntity byId = memberBindRecordDao.selectById(rechargeRecordEntity.getMemberBindId());
        byId.setValidCount(byId.getValidCount() + rechargeRecordEntity.getAddCount())
                .setValidDay(byId.getValidDay() + rechargeRecordEntity.getAddDay())
                .setReceivedMoney(byId.getReceivedMoney().add(rechargeRecordEntity.getReceivedMoney()));
        //memberBind更新
        try {
            memberBindRecordDao.updateById(byId);
        } catch (Exception e) {
            throw new RRException("会员卡信息更新失败");

        }

        //memberLog增加记录
        MemberLogEntity memberLogEntity = new MemberLogEntity();
        memberLogEntity.setType(OperateType.RECHARGE_OPERATION.getMsg())
                .setInvolveMoney(rechargeRecordEntity.getReceivedMoney())
                .setOperator(rechargeRecordEntity.getOperator())
                .setMemberBindId(rechargeRecordEntity.getMemberBindId())
                .setCardCountChange(rechargeRecordEntity.getAddCount())
                .setCardDayChange(rechargeRecordEntity.getAddDay())
                .setNote(rechargeRecordEntity.getNote());
        try {
            memberLogDao.insert(memberLogEntity);
        } catch (Exception e) {
            throw new RRException("会员卡日志信息增加失败");
        }
        return true;
    }

    @Override
    public boolean isDel(Integer id) {
        return false;
    }
}
